package com.ruochen.common.utils;


import android.util.Log;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;

/*
 * 信息加密流程
 * @author m.
 * V 1.0
 * Create at 2020/8/28
 */
public class HYLEncryptionUtil {

    public static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCv1wJ15+FEZ1voy9eU89d39rrYVYv4lz5AxRPGcc2L4KIDQE83Mtn0TvhE/o4Qw00WC3thytTlbkSySn1TV9i4TdXt/wKdrN4Ma/Nkk8R5UjWgzjPpkjglm5afSKs5HwhdLrA4DIpw6fqwsxdfrEjk2u/8AYzJZmNvEW4rNdLxiwIDAQAB";

    //DES密码 8位
    private static final String des_key = "xvMW6yLS";

    /**
     * Http信息加密
     *
     * @param jsonData
     * @return
     */
    public static String encryption(String jsonData) {
        try {
            //时间戳
            long timestamp = System.currentTimeMillis();

            //请求号 随机码 客户端随机生成（32位 用当前时间戳生成MD5）
            String req = MD5Utils.md5(String.valueOf(timestamp));

            //请求内容DES加密
            String biz_content = DESUtils.encrypt(jsonData, des_key);

            //签名 MD532位
            String sign = MD5Utils.md5(biz_content + req + timestamp + des_key);

            //DES密码RSA加密 转base64
            String ridEntry = Base64Util.encodeToString(RSAUtils.encryptByte(des_key, publicKey));

            //包装字段 生成json
            HashMap<String, Object> params = new HashMap<>();
            params.put("ridEntnry", ridEntry);
            params.put("biz_content", biz_content);
            params.put("sign", sign);
            params.put("reqNo", req);
            params.put("timestamp", timestamp);
            String json = null;
            try {
                json = JSON.toJSON(params).toString();
            } catch (Exception e) {
                e.printStackTrace();
                json = "";
            }
            //json结果转base64
            return Base64Util.encodeToString(json.getBytes());
        } catch (Exception e) {
            Log.e("HYLEncryptionUtil", "信息加密失败！" + e.getMessage());
        }
        return "";
    }
}
